package com.xpgk.mvc.btoShop;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.constant.ConstantRender;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.bentryRegister.CustomerProduct;
import com.xpgk.mvc.bentryRegister.CustomerProductSurplus;
import com.xpgk.mvc.binShopService.CustomerService;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.productSales.ProductSales;
import com.xpgk.tools.ToolSqlXml;

public class BoutShopController extends  BaseController{
	
	
	private  BoutShopService   boutShopService;
	public  void   index()
	{	
		Organization  org=Organization.dao.findById(getCUserOrganIds());
		Organization  parentOrg=Organization.dao.findById(org.getStr(Organization.column_parentIds));
		setAttr("organization", org);
		setAttr("parentOrg", parentOrg);
		render("boutShop.html");
	}
	
	/**获取顾客信息
	 * 
	 */
	public  void  getCustomerInfo()
	{
		String  customerIds=getPara("customerIds");
    	Customer  cus=Customer.dao.findById(customerIds);
    	if(cus!=null)
    	{	
    		CustomerDetail  cd=cus.getCustomerDetail();
    		float  sc_weight=cd.getFloat(CustomerDetail.column_firstWeight);
    		float  now_weight=cd.getFloat(CustomerDetail.column_nowWeight);
    		//获取到店次数
    		CustomerService  cscount=Customer.dao.getCustomerServiceCount(customerIds, getCUserOrganIds());
    		long count=cscount.getLong("count");
    		//获取到店服务记录
    		List<CustomerService>  csList=Customer.dao.getCustomerService(customerIds, getCUserOrganIds());
//    		String date=csList.get(0).getTimestamp(CustomerService.column_date).toString();
    		 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    		 String dateString=formatter.format(cd.getDate(CustomerDetail.column_firstDate));
    		if(csList!=null && csList.size()>0)
    		{
    			 dateString = formatter.format(csList.get(0).getTimestamp(CustomerService.column_createDate));
    		}
    		 Organization  org=Organization.dao.findById(getCUserOrganIds());
    		 Organization  parentOrg=Organization.dao.findById(org.getStr(Organization.column_parentIds));
	    		setAttr("organization", org);
	    		setAttr("parentOrg", parentOrg);
	    		setAttr("yijianWeight", sc_weight-now_weight);
	    		setAttr("count",count);
	    		setAttr("scDate", dateString);
	    		setAttr("customer", cus);
    	}
    	render("outShop.html");
	}
	
	/**获取顾客信息
	 * 
	 */
	public  void  showOutInfo()
	{
		String  customerIds=getPara("customerIds");
    	Customer  cus=Customer.dao.findById(customerIds);
    	if(cus!=null)
    	{	
    		CustomerDetail  cd=cus.getCustomerDetail();
    		float  sc_weight=cd.getFloat(CustomerDetail.column_firstWeight);
    		float  now_weight=cd.getFloat(CustomerDetail.column_nowWeight);
    		BtoShop  bs=BtoShop.dao.getOutShop(customerIds,  getCUserOrganIds());
    		//BtoShop  bs=BtoShop.dao.getIntoShop(customerIds, getCUserOrganIds());
    		if(bs!=null)
    		{	
        		 Organization  org=Organization.dao.findById(bs.getStr(BtoShop.column_outShopIds));
        		 Organization  rorg=Organization.dao.findById(bs.getStr(BtoShop.column_intoShopIds));
        		setAttr("reason", bs.getStr("reason"));
        		setAttr("organization", org);
        		setAttr("intoorganization", rorg);
        		setAttr("btoshop", bs);
    		}
	    		setAttr("yijianWeight", sc_weight-now_weight);
	    		setAttr("customer", cus);
    	}
    	render("/bintoShop/adviceNote.html");
	}
	/**获取顾客产品
	 * 
	 */
	public void getCusPro()
	{	
		String  cusIds=getPara("customerIds");
		String  orgids=getCUserOrganIds();
		List<Record>  cpList=new ArrayList<>();
		if(cusIds!="")
		{
			Map<String, Object> queryParam = new HashMap<String, Object>();
			queryParam.put("orgids", orgids);
			queryParam.put("customerID", cusIds);
			LinkedList<Object> paramValue = new LinkedList<Object>();
			String sql =  ToolSqlXml.getSqlMy(CustomerProduct.sqlId_selByCusAndOrg, queryParam, ConstantRender.sql_renderType_beetl, paramValue);
			cpList = Db.find(sql, paramValue.toArray());
		}
		renderJson(cpList);
	}
	/**保存转店记录
	 * 
	 */
	@Before(Tx.class)
	public  void  saveOutShop()
	{
		BtoShop  bs=getModel(BtoShop.class);
		
		List<CustomerProductSurplus>  cpsList=getModels(CustomerProductSurplus.class);
		
		List<ProductSales> psList = getModels(ProductSales.class);
		String msg=boutShopService.saveOutShop(bs,cpsList,psList,getCUserOrganIds(),getCUserIds());
		renderText(msg);
	}
	
}
